#include <iostream>
#include <sstream>
#include <string>

// include nyu header
#include <nyu/nyu.hpp>

int main(int argc, char** argv) {
    // create context
    nyu::Context c;

    // execute nyu script file and get result
    nyu::Handle<nyu::Value> v(c.evalFile("script.nyu"));

    // check value type
    if (!nyu::is_object(v)) {
        std::cerr << "script result must be object!" << std::endl;
        return 1;
    }

    // cast to nyu::Object
    nyu::Object* o = nyu::value_to_object(v);
    if (!o) {
        std::cerr << "the object is null!" << std::endl;
        return 1;
    }

    // get data
    nyu::Value tmp = o->getSlot("val1", c);
    std::string val1;
    switch (nyu::value_tag(tmp)) {
    case nyu::VALUE_TAG_INT:
        {
            std::ostringstream ost;
            ost << nyu::value_to_int(tmp);
            val1 = ost.str();
        }
        break;

    case nyu::VALUE_TAG_OBJECT:
        if (!nyu::is_null(tmp)) {
            nyu::Handle<nyu::String> s(nyu::value_to_object(tmp)->toString(c));
            val1 = s->strval();
        }
        break;
    }

    // OKOK
    std::cout << "val1=" << val1 << std::endl;

    return 0;
}
